authApp.value('page','school_dataFill');
authApp.controller("monitorSeeMapController",function($scope,$http,$location,$getQueryString,$splice,Upload,$timeout){
    var fileLists = [];
    var requestJson={'fid':$getQueryString("pid")};
    var idAll = "";
    var fileList = '';
    var lengthData = '';
    var checksleng = [];//处理多选
    var old_conf = '';

    $scope.state_btn = false;
    $scope.saveDataState=function() {

        $http({
            method: 'get',
            url: apis.querySchoolState,
            data: '',
        }).success(function (response) {
            if (response.code == "000000") {
                if (response.data.s_state == "1") {
                    $scope.state_btn = false;
                } else if (response.data.s_state == "2") {
                    $scope.state_btn = true;
                }
            } else {
                alert(response.errorMsg);
            }
        }).error(function () {
            alert("服务器打瞌睡了，请稍等！");
        })
    }

    //保存学校填报的数据
    $scope.saveData=function(){
        /*for(var key in checksleng){
            var keyIndex = key;
            for(var leng = 0;leng<checksleng[key]-1;leng++){
                keyIndex++;
                if($scope.conf[keyIndex]=="" || $scope.conf[keyIndex]==undefined || $scope.conf[keyIndex]==null){
                    $scope.conf[keyIndex] = "0";
                }
                if($scope.conf[key]=="" || $scope.conf[key]==undefined || $scope.conf[key]==null){
                    $scope.conf[key] = "0";
                }
                $scope.conf[key] += "/"+$scope.conf[keyIndex];
            }
            $scope.conf.splice(key+1,checksleng[key]-1);
        }*/


        for(var key in fileLists){
            //alert(key);//“键名”
            //alert(a[key]);//“键值'
            $scope.conf[key] = fileLists[key];
        }
        if(window.confirm("是否确定提交？")){

            old_conf = $scope.conf;
            for(var key in checksleng){
                var keyIndex = key;
                for(var leng = 0;leng<checksleng[key]-1;leng++){
                    keyIndex++;
                    if(old_conf[keyIndex]=="" || old_conf[keyIndex]==undefined || old_conf[keyIndex]==null){
                        old_conf[keyIndex] = "0";
                    }
                    if(old_conf[key]=="" || old_conf[key]==undefined || old_conf[key]==null){
                        old_conf[key] = "0";
                    }
                    old_conf[key] += "/"+old_conf[keyIndex];
                }
                old_conf.splice(key+1,checksleng[key]-1);
            }
            $scope.conf = old_conf;

            if($scope.conf.indexOf("") != "-1" || $scope.conf.indexOf(undefined) != "-1"){
                alert("请填写未填项");
                return;
            }

            if($scope.conf.length != lengthData){
                alert("请填写未填项");
                return;
            }

            var valueAll = $scope.conf.join("#");
            var rJson={'itemId':idAll,'value':valueAll};
            $http({
                method:'post',
                url:apis.saveObservationPointOrSchoolItemData,
                data:rJson,
            }).success(function(response){
                if(response.code=="000000"){
                    $scope.getAML();
                }else{
                    alert(response.errorMsg);
                }
            }).error(function(){
                alert("服务器打瞌睡了，请稍等！");
            })
        }
    }

    $scope.getAML=function(){
        $http({
            method:'post',
            url:apis.queryObservationPointOrSchoolItemData,
            data:requestJson,
        }).success(function(response){
            if(response.code=="000000"){
                $scope.list=response.data;

                $scope.params = $scope.list;
                $scope.conf = [];
                lengthData = $scope.list.length;

                angular.forEach($scope.list,function(data,index,array){

                    fileList = index;
                    idAll += data.p_id+"#";

                    $scope.uploadFileName = data.price;
                    data.listItemsname = "listname" + index;
                    if(data.p_type == 'radio' || data.p_type == 'select'){
                        data.radioNum = index;
                        data.listItems = data.s_value.split("/");
                        checksleng[index] = data.listItems.length;
                    }
                    if(data.p_type == 'select'){
                        data.checkValues = data.price.split("/");
                        var temp_index = index;
                        for(var temp=0;temp<data.checkValues.length;temp++){
                            if(data.checkValues[temp] == "0"){
                                $scope.conf[temp_index] = 0;
                            }else{
                                $scope.conf[temp_index] = 1;
                            }
                            temp_index++;
                        }
                    }else{
                        $scope.conf[index] = data.price;
                    }
                })
                idAll = idAll.substring(0,idAll.length-1);
            }else{
                alert(response.errorMsg);
            }
        }).error(function(){
            alert("服务器打瞌睡了，请稍等！");
        })
    }

    $scope.getAML();
    $scope.saveDataState();
    //上传pdf文件
    $scope.uploadFiles= function(file, errFiles,index) {
        $scope.f = file;
        $scope.errFile = errFiles && errFiles[0];

        if (file) {
            file.upload = Upload.upload({
                url: apis.uploadfile,
                data: {file: file}
            });

            file.upload.then(function (response) {
                $timeout(function () {
                    if(response.data.code=="000000"){
                        $scope.uploadFileName = response.data.data;

                        fileLists[fileList] = response.data.data;
                    }
                    else
                        alert(response.data.errorMsg);
                });
            }, function (response) {
                if (response.status > 0)
                    $scope.errorMsg = response.status + ': ' + response.data;
            }, function (evt) {
                file.progress = Math.min(100, parseInt(100.0 *
                    evt.loaded / evt.total));
            });
        }
    }

    //上传图片
    $scope.uploadPics= function(file, errFiles,index) {
        $scope.f = file;
        $scope.errFile = errFiles && errFiles[0];

        if (file) {
            file.upload = Upload.upload({
                url: apis.pic,
                data: {file: file}
            });

            file.upload.then(function (response) {
                $timeout(function () {
                    if(response.data.code=="000000"){
                        $scope.uploadePicname = response.data.data;
                    }
                    else
                        alert(response.data.errorMsg);
                });
            }, function (response) {
                if (response.status > 0)
                    $scope.errorMsg = response.status + ': ' + response.data;
            }, function (evt) {
                file.progress = Math.min(100, parseInt(100.0 *
                    evt.loaded / evt.total));
            });
        }
    }

    //校验电话号码
    $scope.checkDate= function(value) {
        var regphone =/^\d{4}-\d{2}-\d{2}/;
        if(value != ""){
            if(!regphone.test(value)){
                alert("您输入的日期格式不正确，请重新输入");
                return "";
            }else{
                return value;
            }
        }
        return "";
    }
   /* function checkPhone(value){
        var regphone =/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(14[0-9]{1}))+\d{8})$/;
        if(value != ""){
            if(!regphone.test(value)){
                mAlert("您输入的手机号码不正确，请重新输入");
                return "";
            }else{
                return value;
            }
        }
        return "";
    }*/
})